/**
 *   Copyright (c) fanguangping. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
 *   which can be found in the file epl-v10.html at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/
package org.jfunny.core.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * 文件工具类
 * 
 * @author fanguangping
 *
 */
public class FileUtil {

	/**
	 * 读文件
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static String readTxt(String filePath) throws IOException {
		InputStream is = FileUtil.class.getResourceAsStream("/" + filePath);
		if (is == null) {
			try {
				is = new FileInputStream(new File(filePath));
			}
			catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		if (is == null) {
			return null;
		}
		BufferedReader br = new BufferedReader(new InputStreamReader(is));

		String str = "";
		String r = br.readLine();
		r = StringUtil.removeUTF8BOM(r);
		while (r != null) {
			str += r + "\n";
			r = br.readLine();
		}
		br.close();
		return str;
	}

	/**
	 * 写文件
	 * @param filePath
	 * @param txt
	 * @throws IOException
	 */
	public static void writeTxt(String filePath, String txt) throws IOException {
		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
		BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
		bw.write(txt);
		bw.close();
	}

	public static int countFile(String filePath) {
		File file = new File(filePath);
		if (file.isDirectory()) {
			return file.listFiles().length;
		}
		else {
			return 1;
		}
	}

	public static String getConfigValue(String filePath, String config) {
		InputStream is = FileUtil.class.getResourceAsStream("/" + filePath);
		if (is == null) {
			try {
				is = new FileInputStream(new File(filePath));
			}
			catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		if (is == null) {
			return null;
		}

		try {
			InputStream in = new BufferedInputStream(is);
			Properties p = new Properties();
			p.load(in);

			return p.getProperty(config);
		}
		catch (Exception e) {
			return null;
		}
	}

	public static void main(String[] args) {
		String className = getConfigValue("sys.properties", "SysEvaluator");
		System.out.println(className);

		/*
		try {
			String txt = readTxt("helloworld/helloworld.jfunny");
			System.out.println(txt);

			System.out.println("=====================");

			txt = readTxt("helloworld/helloworld.tpl");
			System.out.println(txt);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		*/
	}
}
